package com.cheyun.netsalev3.repository

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.dm.DmClue
import com.cheyun.netsalev3.bean.dm.DmClueList
import com.cheyun.netsalev3.bean.dm.DmMsgNum
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.*
import com.cheyun.netsalev3.utils.api.dm.DmApiCallback
import com.cheyun.netsalev3.utils.api.dm.DmApiException
import com.google.gson.Gson
import com.jeremyliao.liveeventbus.LiveEventBus
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.json.JSONObject

class DmPageRepository(var planType: Int,var dealed:Int,var searchKey:String,var status:Int) :BasePageRepository<DmClueList>() {

    override fun loadInitial(page: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<DmClueList>) -> Unit) {
        var json = JSONObject()
        if(planType>0) {
            json.put("planType", planType)
        }
//        json.put("dealed",dealed)

//        var titles = arrayListOf("全部", "待处理", "有效","无效", "待分配")
//        var status = arrayListOf(1, 2, 3, 4,5)

        if(status == 1){
            var isMgr:Int = MySharedPreferences.getDMMgr()
            if(isMgr == 1){
                json.put("dealed_and_all",1)
            }else {
                json.put("dealed", 1)
            }
        }
        else if(status == 2){
            json.put("status",0)
            json.put("dealed",1)
        }else if(status == 3){
            json.put("status",2)
            json.put("dealed",1)
        }else if(status == 4){
            json.put("status",3)
            json.put("dealed",1)
        }else if(status == 5){
            json.put("dealed",0)
        }
        if(!searchKey.equals("")){
            json.put("searchKey",searchKey);
        }
        json.put("device","mobile");
        DmApiClient.getInstance().getClue(json.toString(),1, DmApiStores.PAGE_SIZE)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<DmClue,Any>>() {
                    override fun onSuccess(t: DmBaseResp<DmClue,Any>) {
                        t.list?.rows?.apply {
                            total.postValue(t.list!!.total)
                            callback(t.list!!.rows)
                        }
                        if(t.data!=null){
                            val str = Gson().toJson(t.data!!);
                            var msgNum = Gson().fromJson(str, DmMsgNum::class.java)
                            if(msgNum!=null){
                                LiveEventBus.get().with("xktNum").postValue(msgNum.dealing.p_1+msgNum.waiting.p_1)
                                LiveEventBus.get().with("zhtNum").postValue(msgNum.dealing.p_2+msgNum.waiting.p_2)
                                LiveEventBus.get().with("cztNum").postValue(msgNum.dealing.p_3+msgNum.waiting.p_3)

                                LiveEventBus.get().with("xkt_all").postValue(msgNum.dealing.p_1+msgNum.waiting.p_1)
                                LiveEventBus.get().with("xkt_wait").postValue(msgNum.waiting.p_1)
                                LiveEventBus.get().with("xtk_dealing").postValue(msgNum.dealing.p_1)

                                LiveEventBus.get().with("zht_all").postValue(msgNum.dealing.p_2+msgNum.waiting.p_2)
                                LiveEventBus.get().with("zht_wait").postValue(msgNum.waiting.p_2)
                                LiveEventBus.get().with("zht_dealing").postValue(msgNum.dealing.p_2)

                                LiveEventBus.get().with("czt_all").postValue(msgNum.dealing.p_3+msgNum.waiting.p_3)
                                LiveEventBus.get().with("czt_wait").postValue(msgNum.waiting.p_3)
                                LiveEventBus.get().with("czt_dealing").postValue(msgNum.dealing.p_3)
                            }
                        }
                    }

                    override fun onError(ex: DmApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }
                })
    }

    override fun loadAfter(page: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, DmClueList>) {
        var json = JSONObject()
        if(planType>0) {
            json.put("planType", planType)
        }
//        json.put("dealed",dealed)

//        var titles = arrayListOf("全部", "待处理", "有效","无效", "待分配")
//        var status = arrayListOf(1, 2, 3, 4,5)
        if(status == 2){
            json.put("status",0)
        }else if(status == 3){
            json.put("status",2)
        }else if(status == 4){
            json.put("status",3)
        }else if(status == 5){
            json.put("dealed",0)
        }
        if(!searchKey.equals("")){
            json.put("searchKey",searchKey);
        }
        json.put("device","mobile");
        DmApiClient.getInstance().getClue(json.toString(),page.key, DmApiStores.PAGE_SIZE)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<DmClue,Any>>() {
                    override fun onSuccess(t: DmBaseResp<DmClue,Any>) {
                        t.list?.rows?.apply {
                            callback.onResult(this, page.key + 1)
                        }
                    }

                    override fun onError(ex: DmApiException) {

                    }
                })
    }

    override fun loadBefore(page: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, DmClueList>) {
        var json = JSONObject()
        if(planType>0) {
            json.put("planType", planType)
        }
//        json.put("dealed",dealed)

//        var titles = arrayListOf("全部", "待处理", "有效","无效", "待分配")
//        var status = arrayListOf(1, 2, 3, 4,5)
        if(status == 2){
            json.put("status",0)
        }else if(status == 3){
            json.put("status",2)
        }else if(status == 4){
            json.put("status",3)
        }else if(status == 5){
            json.put("dealed",0)
        }
        if(!searchKey.equals("")){
            json.put("searchKey",searchKey);
        }
        json.put("device","mobile");
        DmApiClient.getInstance().getClue(json.toString(), page.key, DmApiStores.PAGE_SIZE)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : DmApiCallback<DmBaseResp<DmClue,Any>>() {
                    override fun onSuccess(t: DmBaseResp<DmClue,Any>){
                        t.list?.rows?.apply {
                            callback.onResult(this, page.key - 1)
                        }
                    }

                    override fun onError(ex: DmApiException) {

                    }
                })
    }


}